var CONTAO_SAVING = 'Daten werden gespeichert';
var CONTAO_ERROR = 'Daten werden gespeichert';

var M17BackendTools;
M17BackendTools = new Class({
    Implements:[Options, Events],
    options:{
        'css':{
            'stickyClass':'stickySave'
        }
    },
    initialize:function (options) {
        this.setOptions(options);
        this.url = new URI(location.href);
    },
    loadAlways: function() {
        this.removeArticleEditHide();
        this.showInformations();
        this.stickyFooter.init();
        if(this.url.getData('showOnlyMain') && parent.document){
            this.SaveAndCloseLightbox();
        }
    },
    showInformations: function() {
        return $$('#main a img[src$=show.gif]').each(function(el){
            this.addLightboxToElement(el.getParent('a'), 'showInformations');
        }, this);
    },
    SaveAndCloseLightbox: function() {
        $$('.tl_form').each(function(el){
            el.set('action', el.get('action')+'&reload=1');
            if(this.url.getData('reload') && $$('#main form .error').length < 1)
            {
                parent.location.href = parent.location.href;
            }
        },this);
    },
    /* sticky footer wrapper
     * @desc initialize the stickyFooter for Backend
     */
    stickyFooter:{
        init: function() {
            var formBody = $$('.tl_formbody_submit')[0] || false,
                pos,
                difference;

            //invoke only when element is found.
            if(formBody) {
                var pos = formBody.getPosition().y + formBody.getHeight();
                var difference = pos - window.getHeight();
                //load the methoad with init
                this.setItem(difference);
                window.addEvent('scroll', function(){
                    this.setItem(difference);
                }.bind(this));
            }
        },
        /* initStickyFooter
         * @desc initialize the stickyFooter for Backend
         */
        setItem: function(diff) {
            var $this = window,
            $body = document.body,
            stickyClass = 'stickySave';
            var scrollSizeToInt = $this.getScroll().y.toInt();
            if(scrollSizeToInt > diff) {
                $body.removeClass(stickyClass);
            } else {
                //check if class set
                if(!$body.hasClass(stickyClass)) {
                    $body.addClass(stickyClass);
                }
            }
        }
    },
    removeArticleEditHide:function () {
        $$('#main .edit-header').each(function (el) {
            el.removeClass('invisible');
            this.addLightboxToElement(el, 'showArticle');
        }, this);
    },
    addLightboxToElement: function(el, name) {
        var _params = name+' 765 80%';

        el.set({
            'data-lightbox':_params,
            'href':el.get('href') + '&showOnlyMain=1'
        });
    },
    colSave: function(elSet) {
        elSet.addEvent('click', function(e){
            e.preventDefault();
            var _href = this.get('href');
            var _hrefLabel = this.get('text');
            var _howdy = this.getParent('.tl_left').getElement('.currentCol');
            var _parent = this.getParent('ul');
            var xhr = new Request.HTML({
                'url': _href,
                onRequest: AjaxRequest.displayBox(CONTAO_SAVING + ' …'),
                onFailure: AjaxRequest.displayBox(CONTAO_ERROR + '!'),
                onSuccess: function(responseText, responseXML) {
                    AjaxRequest.hideBox();
                    _howdy.set('text', _hrefLabel);
                }
            });
            xhr.send();
        });
    },
    addDragADrop: function() {
        $$('.tl_listing .tl_file').each(function(el) {
            var moveItem = new Element('img', {'src': 'system/modules/m17BackendTools/html/img/icon/arrow-move.png','alt': 'move', 'class': 'movethis'});
            moveItem.inject(el.getElement('.tl_left a') ,'before');
        });


        $$('.tl_listing .tl_file').addEvent('mousedown', function(event) {
            event.stop();
            $this = this;
            if(event.target.hasClass('movethis')) {

                var cloneer = $this.getElement('.tl_left').clone().setStyles(this.getCoordinates()).setStyles({
                      opacity: 0.7,
                      position: 'absolute'
                    }).inject(document.body);
                cloneer.getElement('.rtz').setStyle('display', 'none');
                var drag = new Drag.Move(cloneer, {

                  droppables: $$('.tl_listing .tl_file', '.tl_listing .tl_folder'),

                  onDrop: function(dragging, goal){
                      dragging.destroy();
                      goal.setStyle('outline', 'none');
                      $this.inject(goal,'after');
                      var _padding;
                      var doRequest = function(mode,pid,id) {
                        var _xhr = new Request({
                          'url': 'http://cto2111.local/contao/main.php?do=article&act=cut&mode=1&pid=17&id=10'
                        });
                      };

                      if(goal.hasClass('tl_folder')) {
                          _padding = goal.getElement('.tl_left').getStyle('paddingLeft').toInt() + 40;
                      } else {
                          _padding = goal.getElement('.tl_left').getStyle('paddingLeft').toInt();
                      }

                      $this.getElement('.tl_left').setStyles({
                          'paddingLeft': _padding
                      });


                  },
                  onEnter: function(dragging, goal){
                        goal.setStyle('outline', '2px dotted #8AB858');
                  },
                  onLeave: function(dragging, goal){
                      goal.setStyle('outline', 'none');
                  },
                  onCancel: function(dragging){
                      dragging.destroy();
                  }
                });

                    drag.start(event);
            }

        });
    }
});
//var BE_TOOLS;
var BE_TOOLS = new M17BackendTools();
window.addEvent('domready', function(){

    BE_TOOLS.loadAlways();
});

//console.log(BE_TOOLS);
